#ifndef CPAGE_H
#define CPAGE_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlQueryModel>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
class CPage : public QObject
{
    Q_OBJECT
public:
    explicit CPage(QObject *parent = nullptr);
    CPage(const QString &tbname,
          const QSqlDatabase &db = QSqlDatabase(),
          QObject *parent = nullptr,
          const QString &dbname=QString(),const QStringList &filterList = QStringList());
    QSqlQueryModel* model();
    void doSearch(const int page,const QString& keyword,const int pageSize=15); //默认为过滤name和alias
    void exportTable(const QString& filePath);
    QJsonObject recordToJson(const QSqlRecord &record);
    void refresh();



protected:
    QSqlDatabase m_db;
    QString m_tbname,m_dbname;
    QString m_condition = QString();
    int m_cur_page = 0; //当前页 -1为没有记录
    int m_total_page = 0; //共有多少条记录
    int m_per_page; //每页多少条记录 默认15
    QSqlQueryModel *m_model;
    QStringList m_filterList;
    QJsonArray getRows(QString strCondition = QString());




signals:
    void searchFinished(int totalPage,int curPage,int totalRecord);

};

#endif // CPAGE_H
